import type { RequiredDeep } from 'type-fest'

import { ajax } from '@/utils'

import type { components } from '../schema'
import type { IDeleteReq, IExportRes } from '../type'

/**
 * 字典接口
 */
export const dictApi = {
  /**
   * 获取字典类型分页
   */
  getDictTypePage(data: IGetDictTypePageReq): Promise<IGetDictTypePageRes> {
    return ajax.post('/system/dict/type/page', data)
  },
  /**
   * 添加字典类型
   */
  createDictType(data: ICreateDictTypeReq) {
    return ajax.post('/system/dict/type/create', data)
  },
  /**
   * 更新字典类型
   */
  updateDictType(id: string, data: IUpdateDictTypeReq) {
    return ajax.put('/system/dict/type/update', data, { params: { id } })
  },
  /**
   * 删除字典类型
   */
  deleteDictType(data: IDeleteReq) {
    return ajax.delete('/system/dict/type/delete', { data })
  },
  /**
   * 获取字典类型
   */
  getDictType(id: string): Promise<IGetDictTypeRes> {
    return ajax.get('/system/dict/type', { params: { id } })
  },
  /**
   * 导出字典类型分页
   */
  exportDictTypePage(data: IGetDictTypePageReq): Promise<IExportRes> {
    return ajax.post('/system/dict/type/export', data)
  },
  /**
   * 获取字典数据分页
   */
  getDictDataPage(data: IGetDictDataPageReq): Promise<IGetDictDataPageRes> {
    return ajax.post('/system/dict/data/page', data)
  },
  /**
   * 获取字典数据列表
   */
  getDictDataList(dictType: string): Promise<IGetDictDataListRes> {
    return ajax.get('/system/dict/data/list', { params: { dictType } })
  },
  /**
   * 添加字典数据
   */
  createDictData(data: ICreateDictDataReq) {
    return ajax.post('/system/dict/data/create', data)
  },
  /**
   * 更新字典数据
   */
  updateDictData(id: string, data: IUpdateDictDataReq) {
    return ajax.put('/system/dict/data/update', data, { params: { id } })
  },
  /**
   * 删除字典数据
   */
  deleteDictData(data: IDeleteReq) {
    return ajax.delete('/system/dict/data/delete', { data })
  },
  /**
   * 导出字典数据分页
   */
  exportDictDataPage(data: IGetDictDataPageReq): Promise<IExportRes> {
    return ajax.post('/system/dict/data/export', data)
  }
}

export type IGetDictTypePageReq = components['schemas']['FindDictTypePageDTO']
export type IGetDictTypePageRes = RequiredDeep<
  components['schemas']['FindDictTypePageVO']['data']
>
export type IDictTypePageItem = IGetDictTypePageRes['list'][0]
export type ICreateDictTypeReq = components['schemas']['CreateDictTypeDTO']
export type IUpdateDictTypeReq = components['schemas']['UpdateDictTypeDTO']
export type IGetDictTypeRes = RequiredDeep<
  components['schemas']['GetDictTypeVO']['data']
>
export type IGetDictDataPageReq = components['schemas']['FindDictDataPageDTO']
export type IGetDictDataPageRes = RequiredDeep<
  components['schemas']['FindDictDataPageVO']['data']
>
export type IDictDataPageItem = IGetDictDataPageRes['list'][0]
export type IGetDictDataListRes = RequiredDeep<
  components['schemas']['GetDictDataListVO']['data']
>
export type ICreateDictDataReq = components['schemas']['CreateDictDataDTO']
export type IUpdateDictDataReq = components['schemas']['UpdateDictDataDTO']
